package violentRecursion;

/**
 * @className: LeetCode_123
 * @Description: 买卖股票的最佳时机 III
 * @author: kunkun
 * @date: 2025/7/10 21:07
 *
 */
class LeetCode_123 {
    public int maxProfit(int[] prices) {
        return solution_2(prices,0,2,true);
    }

    private int solution_2(int[] prices, int x, int i1, boolean b) {
        int[][] dp1=new int[prices.length+1][3];       //true
        int[][] dp2=new int[prices.length+1][3];        //false
        for (int i = prices.length-1; i >= 0; i--) {
            dp1[i][0]=dp1[i+1][0];
            dp1[i][1]=Math.max(dp2[i+1][0]-prices[i],dp1[i+1][1]);
            dp1[i][2]=Math.max(dp2[i+1][1]-prices[i],dp1[i+1][2]);
            dp2[i][0]=Math.max(dp1[i+1][0]+prices[i],dp2[i+1][0]);
            dp2[i][1]=Math.max(dp1[i+1][1]+prices[i],dp2[i+1][1]);
            dp2[i][2]=Math.max(dp1[i+1][2]+prices[i],dp2[i+1][2]);
        }
        return dp1[0][2];
    }

    private int solution_1(int[] prices, int i, int sum, boolean flag) {
        if (i==prices.length){
            return 0;
        }
        if (flag){
            return Math.max(
                    (sum>0)?solution_1(prices,i+1,sum-1,false)-prices[i]:Integer.MIN_VALUE,
                    solution_1(prices,i+1,sum,true));
        }else {
            return Math.max(
                    solution_1(prices,i+1,sum,true)+prices[i],
                    solution_1(prices,i+1,sum,false));
        }
    }
}
